'''
Created on Feb 9, 2012

@author: xzhu
'''
import ioniState, stateHead

class allState():
    '''
    classdocs
    '''
    # initialize this class variable first
    sHead = stateHead.stateHead()
    
    def __init__(self):
        '''
        Constructor
        '''
        self.states = []
        
    def load_all_states(self, ifile):
        '''
        load all the possible ionization states from a file
        '''
        lines = open(ifile).readlines()
        
        # the first line is the header
        allState.sHead.init_from_line(lines[0])
        lines.pop(0)
        
        count = 0
        for line in lines:
            buff_state = ioniState.ioniState()
            buff_state.load_state(line)
            buff_state.seq = count
            count += 1
            self.states.append(buff_state)
            
    def state_of_crg(self, crgs):
        '''
        return the state which has the same charges as crgs
        '''
        #print 'len  ', len(self.states)
        for state in self.states:
            if state.crgs == crgs:
                return state
           
        print 'can not find the crgs', crgs
        
            
    def write_seq_ID(self, outfile = 'allStates_seq_ID.txt'):
        '''
        output the seq and ID relationship of all the states
        '''
        
        ofile = open(outfile, 'w')
        for state in self.states:
            print state.ID, state.seq
            ofile.write(str(state.seq) + '\t' + state.ID + '\n')
        ofile.close()
        